from django.shortcuts import render, HttpResponse
import os

from .forms import NameForm


def test(request):
    path = request.GET.get("path")
    print(path)
    files = os.listdir(path)
    data = [{"name": f, "path": os.path.join(path, f), "is_file": os.path.isfile(os.path.join(path,f))} for f in files]
    return render(request, "index.html", {'data': data})


def get_name(request):
    if request.method == "POST":
        print("POST")
        form = NameForm(request.POST, request.FILES)
        if form.is_valid():
            your_name = form.cleaned_data["your_name"]
            data = request.FILES['file']
            with open("a.html", "wb+") as dest:
                for chunk in data.chunks():
                    dest.write(chunk)
            return HttpResponse(f"thanks: {your_name}")
    else:
        form = NameForm()
    return render(request, "name.html", {"form": form})
